package cn.xhz.gdal.util;

import cn.hutool.core.io.resource.ResourceUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

public class YAMLUtil {
    /**
     * desc: 获取yml值
     * date: 2022-09-08
     * @param ymlPath yml地址 资源文件夹的名称
     * @param cronName yml全称，需要使用点做分隔符
     **/
    public static String getYmlValue(String ymlPath,String cronName) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
        InputStream stream = ResourceUtil.getStream(ymlPath);
        String[] split = cronName.split("\\.");
        Map map= objectMapper.readValue(stream, Map.class);
        Map info = new HashMap();
        String cron = "";
        for (int i = 0; i < split.length; i++) {
            if (i ==0) {
                info = (Map) map.get(split[i]);
            }else  if(i==split.length-1){
                cron = (String) info.get(split[i]);
            }else{
                info = (Map) info.get(split[i]);
            }
            if (info==null) {
                return null;
            }
        }
        return cron;
    }

}
